package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.entity.OrderDetail;
import com.itheima.entity.Orders;
import com.itheima.entity.User;
import com.itheima.service.OrderDetailService;
import com.itheima.service.OrdersService;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("order")
public class OrdersController {
    @Autowired
    OrdersService ordersService;

    @Autowired
    UserService userService;
    @Autowired
    OrderDetailService orderDetailService;
    @PostMapping("submit")
    public R<String> sbmit(@RequestBody Orders orders){

        ordersService.sumbit(orders);

        return R.success("下单成功");
    }

    @GetMapping("userPage")
    public R<Page<Orders>> page(Integer page, Integer pageSize){
        Page<Orders> pageInfo=new Page<>(page, pageSize);
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<Orders> wrapper = Wrappers.lambdaQuery(Orders.class)
                .eq(Orders::getUserId, userId);

        ordersService.page(pageInfo,wrapper);
        return R.success(pageInfo);

    }

    @GetMapping("page")
    public R<Page<OrderDetail>> page2(Integer page, Integer pageSize){
        Page<OrderDetail> pageInfo=new Page<>(page, pageSize);
        Long userId = BaseContext.getCurrentId();
        User user = userService.getById(userId);

        Orders orders = ordersService.getById(user.getId());
        LambdaQueryWrapper<OrderDetail> wrapper = Wrappers.lambdaQuery(OrderDetail.class)
                .eq(OrderDetail::getOrderId, orders.getId());

        orderDetailService.page(pageInfo,wrapper);
        return R.success(pageInfo);

    }

}
